Python Operators
Operators are symbols in Python that are used to manipulate variables and values. Python has a wide range of operators that fall into various categories:
Arithmetic Operators:
Arithmetic operators are used to perform basic mathematical operations.
|
Operator |
Description |
Example |
|
+ |
Addition |
a + b |
|
- |
Subtraction |
a - b |
|
* |
Multiplication |
a * b |
|
/ |
Division |
a / b |
|
// |
Floor
Division (quotient) |
a // b |
|
% |
Modulus
(remainder) |
a % b |
|
** |
Exponentiation |
a ** b |
a = 10
b = 3
print(a + b) # 13 (Addition)
print(a - b) # 7 (Subtraction)
print(a * b) # 30 (Multiplication)
print(a / b) # 3.333 (Division)
print(a // b) # 3 (Floor Division)
print(a % b) # 1 (Modulus)
print(a ** b) # 1000 (Exponentiation)
|
Operator |
Description |
Example |
|
== |
Equal to |
a == b |
|
!= |
Not equal to |
a != b |
|
> |
Greater than |
a > b |
|
< |
Less than |
a < b |
|
>= |
Greater than
or equal to |
a >= b |
|
<= |
Less than or
equal to |
a <= b |
Logical Operators
Logical operators are used to combine conditional statements.
|
Operator |
Description |
Example |
|
and |
Returns True
if both operands are true |
a and b |
|
or |
Returns True
if at least one operand is true |
a or b |
|
not |
Reverses the
logical state |
not a |
Assignment Operators
Assignment operators are used to assign values to variables.
|
Operator |
Description |
Example |
|
= |
Assigns value
to variable |
a = 5 |
|
+= |
Adds and
assigns |
a += 3 |
|
-= |
Subtracts and
assigns |
a -= 3 |
|
*= |
Multiplies
and assigns |
a *= 3 |
|
/= |
Divides and
assigns |
a /= 3 |
|
//= |
Floor divides
and assigns |
a //= 3 |
|
%= |
Modulus and
assigns |
a %= 3 |
|
**= |
Exponentiates
and assigns |
a **= 3 |
Example:
a = 10
a += 5 # a = a + 5 → a = 15
a -= 3 # a = a - 3 → a = 12
a *= 2 # a = a * 2 → a = 24
a /= 4 # a = a / 4 → a = 6.0
Identity operators are used to compare the memory location of two objects.
|
Operator |
Description |
Example |
|
is |
Returns True
if both variables point to the same object in memory |
a is b |
|
is not |
Returns True
if both variables do not point to the same object in memory |
a is not b |
Example:
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a is b) # False (a and b are different objects)
print(a is c) # True (a and c are the same object)
print(a is not b) # True (a and b are different objects)
|
Operator |
Description |
Example |
|
in |
Returns True if the value is found
in the sequence |
a in b |
|
not in |
Returns True if the value is not
found in the sequence |
a not in b |
Bitwise Operators
Bitwise operators are used to perform bit-level operations on integers.
|
Operator |
Description |
Example |
|
& |
Bitwise AND |
a & b |
|
` |
` |
Bitwise OR |
|
^ |
Bitwise XOR |
a ^ b |
|
~ |
Bitwise NOT |
~a |
|
<< |
Bitwise left shift |
a << 2 |
|
>> |
Bitwise right shift |
a >> 2 |
Ternary Conditional Operator (Conditional Expression)
The ternary conditional operator allows you to evaluate a condition and choose between two values based on the result.
|
Operator |
Description |
Example |
|
x if condition else y |
Returns x if the condition is
True, otherwise returns y |
a if a > b else b |
Comments